package edu.kit.csl.pisa.datatypes;

/*
This file is part of the PISA Alignment Tool.

Copyright (C) 2013
Karlsruhe Institute of Technology
Cognitive Systems Lab (CSL)
Felix Stahlberg

PISA is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

PISA is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with PISA. If not, see <http://www.gnu.org/licenses/>.
*/

import java.util.LinkedList;

/**
 * A sentence is a list of word-ids. On the target side, each phoneme has its
 * own word-id. The string representations for these word-ids can be found in
 * dictionaries. We break with the OO paradigm here (by not using pointer to
 * "Word" class instances instead of word-ids) because we have to access them
 * very often and reduce thereby the complexity of this access.
 * 
 * @see Dictionary
 */
public class Sentence {

	/*
	 * Sentence as integer array, where the elements are the word-ids of the
	 * sentence.
	 */
	private int[] s;
	
	/**
	 * This constructor creates a Sentence instance from a blank separated
	 * string representation.
	 * 
	 * @param str sentence as string with blank separated words
	 * @param dictionary which dictionary should be used for converting the
	 * 					words to word-id
	 * @throws NullPointerException if dictionary is null
	 */
	public Sentence(String str, Dictionary dictionary) 
			throws NullPointerException {
		String[] words = str.split("\\s+");
		LinkedList<Integer> wordIds = new LinkedList<Integer>();
		for (String word : words) {
			if (word.isEmpty()) {
				continue;
			}
			wordIds.add(dictionary.get(word));
		}
		// Workaround because of stupid autobox issues with java..
		s = new int[wordIds.size()];
		int i = 0;
		for (int id : wordIds) {
			s[i++] = id;
		}
	}
	
	/**
	 * Get the word-id on the given position.
	 * 
	 * @param pos the position in the sentence
	 * @throws ArrayIndexOutOfBoundsException if the given position can not be
	 * 				found in the sentence 
	 */
	public int get(int pos) {
		return s[pos];
	}
	
	/**
	 * Get sentence length.
	 * 
	 * @return sentence length
	 */
	public int getLength() {
		return s.length;
	}
	
	/**
	 * Converts the sentence to an integer array. Elements in this array
	 * correspond to word-ids.
	 * 
	 * @return the sentence represented as integer array
	 */
	public int[] toArray() {
		return s;
	}
	
	/**
	 * Get the string representation of this sentence. The functionality is
	 * similar to a <code>Object.toString()</code> method, but the dictionary 
	 * has to be given as well.
	 * 
	 * @param dict Dictionary to use
	 */
	public String toString(Dictionary dict) {
		StringBuilder b = new StringBuilder();
		for (int c : s) {
			b.append(dict.get(c) + " ");
		}
		return b.toString();
	}
}
